import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useUserStore = defineStore('user', () => {
  // 状态
  const token = ref(localStorage.getItem('token') || '')
  const userInfo = ref({})
  const permissions = ref([])
  const roles = ref([])

  // 模拟用户数据
  const mockUserInfo = {
    id: 1,
    username: 'admin',
    realName: '系统管理员',
    email: 'admin@example.com',
    avatar: '',
    department: '技术部',
    phone: '13800138000'
  }

  // 模拟权限数据
  const mockPermissions = [
    'purchase:order:list',
    'purchase:order:add',
    'purchase:order:edit',
    'purchase:order:delete',
    'purchase:supplier:list',
    'purchase:supplier:add',
    'purchase:supplier:edit',
    'purchase:supplier:delete',
    'purchase:material:list',
    'purchase:material:add',
    'purchase:material:edit',
    'purchase:material:delete',
    'warehouse:inventory:list',
    'warehouse:inventory:add',
    'warehouse:inventory:edit',
    'warehouse:inventory:delete',
    'warehouse:inbound:list',
    'warehouse:inbound:add',
    'warehouse:inbound:edit',
    'warehouse:inbound:delete',
    'warehouse:outbound:list',
    'warehouse:outbound:add',
    'warehouse:outbound:edit',
    'warehouse:outbound:delete',
    'warehouse:permission:list',
    'warehouse:permission:add',
    'warehouse:permission:edit',
    'warehouse:permission:delete',
    'sales:order:list',
    'sales:order:add',
    'sales:order:edit',
    'sales:order:delete',
    'sales:customer:list',
    'sales:customer:add',
    'sales:customer:edit',
    'sales:customer:delete',
    'sales:product:list',
    'sales:product:add',
    'sales:product:edit',
    'sales:product:delete',
    'finance:account:list',
    'finance:account:add',
    'finance:account:edit',
    'finance:account:delete',
    'finance:invoice:list',
    'finance:invoice:add',
    'finance:invoice:edit',
    'finance:invoice:delete',
    'finance:report:list',
    'system:user:list',
    'system:user:add',
    'system:user:edit',
    'system:user:delete',
    'system:role:list',
    'system:role:add',
    'system:role:edit',
    'system:role:delete',
    'system:permission:list',
    'system:permission:add',
    'system:permission:edit',
    'system:permission:delete',
    'retail:product:list',
    'retail:product:add',
    'retail:product:edit',
    'retail:product:delete',
    'retail:store:list',
    'retail:store:add',
    'retail:store:edit',
    'retail:store:delete',
    'retail:order:list',
    'retail:order:add',
    'retail:order:edit',
    'retail:order:delete',
    'retail:member:list',
    'retail:member:add',
    'retail:member:edit',
    'retail:member:delete',
    'retail:inventory:list',
    'retail:inventory:add',
    'retail:inventory:edit',
    'retail:inventory:delete'
  ]

  // 模拟角色数据
  const mockRoles = ['admin', 'manager', 'operator']

  // 计算属性
  const isLoggedIn = computed(() => !!token.value)
  const hasPermission = (permission) => permissions.value.includes(permission)
  const hasRole = (role) => roles.value.includes(role)

  // 方法
  const login = async (loginForm) => {
    // 模拟登录
    if (loginForm.username === 'admin' && loginForm.password === '123456') {
      const mockToken = 'mock-token-' + Date.now()
      token.value = mockToken
      localStorage.setItem('token', mockToken)
      
      // 获取用户信息
      await getUserInfo()
      
      return { success: true, message: '登录成功' }
    } else {
      return { success: false, message: '用户名或密码错误' }
    }
  }

  const logout = () => {
    token.value = ''
    userInfo.value = {}
    permissions.value = []
    roles.value = []
    localStorage.removeItem('token')
  }

  const getUserInfo = async () => {
    // 模拟获取用户信息
    userInfo.value = mockUserInfo
    permissions.value = mockPermissions
    roles.value = mockRoles
  }

  const initUser = () => {
    if (token.value) {
      getUserInfo()
    }
  }

  return {
    // 状态
    token,
    userInfo,
    permissions,
    roles,
    
    // 计算属性
    isLoggedIn,
    hasPermission,
    hasRole,
    
    // 方法
    login,
    logout,
    getUserInfo,
    initUser
  }
}) 